﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PoolGame2.Player;
using PoolGame2.Managers;

namespace PoolGame2.Menus
{
    public class CreditsMenu : Menu
    {
        public class Credit
        {
            public String name;
            public Vector2 pos;
            public Texture2D image;
            public SpriteFont font;
            public Credit(String n, Vector2 p, SpriteFont f)
            {
                name = n;
                pos = p;
                image = null;
                font = f;
            }

            public Credit(String n, Vector2 p, Texture2D i, SpriteFont f)
            {
                name = n;
                pos = p;
                image = i;
                font = f;
            }
        };
        float yPos = 0.0f;
        float imageTimer = 0.0f;
        int iListPosCur;
        int iPresidentsPos = 0;
        List<Credit> credits;
        Texture2D finalImage;
        float flCreditLastPos = 320;

        public CreditsMenu(PoolGame game)
            : base(game)
        {
            flCreditLastPos = game.graphics.GraphicsDevice.Viewport.Height;
            this.backgroundTexture = game.textureDatabase.GetTexture("creds/credits_bg");

            credits = new List<Credit>();

            QuickAddTitle("CS7010 - Group Project");
            QuickAddTitle("Pool Game", 64);
            QuickAddSubTitle("IET Team 3", 12);
            QuickAddCredit("Finlay McAree");
            QuickAddCredit("Jonathan O'Donovan");
            QuickAddCredit("Barry O'Sullivan");
            QuickAddCredit("A.B.M. Siddiqur Rahman");
            QuickAddCredit("Andrew Ritchie");
            QuickAddCredit("Maosha Shi");
            QuickAddCredit("David Whelan");
            
            QuickAddCredit("", 32);
            QuickAddSubTitle("Special Thanks", 4);
            QuickAddCredit("Call of Duty 4");
            QuickAddCredit("Left 4 Dead");
            QuickAddCredit("Coffee");
            QuickAddCredit("Coffee");
            QuickAddCredit("Coffee");
            QuickAddCredit("Coffee");

            iPresidentsPos = credits.Count;
            QuickAddCredit("", game.graphics.GraphicsDevice.Viewport.Height/2);
            QuickAddSubTitle("Presidents of the United States");
            QuickAddCredit("George Washington" );
            QuickAddCredit("John Adams");
            QuickAddCredit("Thomas Jefferson");
            QuickAddCredit("James Madison");
            QuickAddCredit("James Monroe");
            QuickAddCredit("John Quincy Adams ");
            QuickAddCredit("Andrew Jackson");
            QuickAddCredit("Martin Van Buren");
            QuickAddCredit("William Henry Harrison");
            QuickAddCredit("John Tyler, Jr.");
            QuickAddCredit("James Knox Polk");
            QuickAddCredit("Zachary Taylor");
            QuickAddCredit("Millard Fillmore");
            QuickAddCredit("Franklin Pierce");
            QuickAddCredit("James Buchanan, Jr.");
            QuickAddCredit("Abraham Lincoln");
            QuickAddCredit("Andrew Johnson");
            QuickAddCredit("Ulysses S. Grant");
            QuickAddCredit("Rutherford Birchard Hayes");
            QuickAddCredit("James Abram Garfield");
            QuickAddCredit("Chester Alan Arthur");
            QuickAddCredit("Stephen Grover Cleveland");
            QuickAddCredit("William McKinley, Jr.");
            QuickAddCredit("Theodore Roosevelt");
            QuickAddCredit("William Howard Taft");
            QuickAddCredit("Thomas Woodrow Wilson");
            QuickAddCredit("Warren Gamaliel Harding");
            QuickAddCredit("John Calvin Coolidge, Jr.");
            QuickAddCredit("Herbert Clark Hoover");
            QuickAddCredit("Franklin Delano Roosevelt");
            QuickAddCredit("Harry S. Truman");
            QuickAddCredit("Dwight David Eisenhower");
            QuickAddCredit("John Fitzgerald Kennedy");
            QuickAddCredit("Lyndon Baines Johnson");
            QuickAddCredit("Richard Milhous Nixon");
            QuickAddCredit("Gerald Rudolph Ford, Jr.");
            QuickAddCredit("James Earl Carter, Jr.");
            QuickAddCredit("Ronald Wilson Reagan");
            QuickAddCredit("George Herbert Walker Bush");
            QuickAddCredit("William Jefferson Clinton");
            QuickAddCredit("George Walker Bush");
            QuickAddCredit("Barack Hussein Obama II");
            /*
                credits.Add(new Credit("Peadar", new Vector2(0, 4000)));
                credits.Add(new Credit("Peadar", new Vector2(0, 4125)));
                credits.Add(new Credit("Peadar", new Vector2(0, 4250)));
                credits.Add(new Credit("Peadar", new Vector2(0, 4375)));
                credits.Add(new Credit("Peadar", new Vector2(0, 4500)));
                credits.Add(new Credit("Peadar", new Vector2(0, 4625)));
                credits.Add(new Credit("Peadar", new Vector2(0, 4750)));
                credits.Add(new Credit("Peadar", new Vector2(0, 4875)));
                credits.Add(new Credit("Peadar", new Vector2(0, 5000)));
            */
            finalImage = game.textureDatabase.GetTexture("creds/credits_end");
        }

        private void QuickAddTitle(String credit, float gap)
        {
            SpriteFont font = ((PoolGame)Game).fontDatabase.loadFont("CourierNewTitle");
            credits.Add(new Credit(credit, new Vector2(0, flCreditLastPos), font));
            flCreditLastPos += (gap + font.LineSpacing);
        }

        private void QuickAddTitle(String credit)
        {
            SpriteFont font = ((PoolGame)Game).fontDatabase.loadFont("CourierNewTitle");
            credits.Add(new Credit(credit, new Vector2(0, flCreditLastPos), font));
            flCreditLastPos += font.LineSpacing;
        }

        private void QuickAddSubTitle(String credit, float gap)
        {
            SpriteFont font = ((PoolGame)Game).fontDatabase.loadFont("CourierNewSubTitle");
            credits.Add(new Credit(credit, new Vector2(0, flCreditLastPos), font));
            flCreditLastPos += (gap + font.LineSpacing);
        }

        private void QuickAddSubTitle(String credit)
        {
            SpriteFont font = ((PoolGame)Game).fontDatabase.loadFont("CourierNewSubTitle");
            credits.Add(new Credit(credit, new Vector2(0, flCreditLastPos), font));
            flCreditLastPos += font.LineSpacing;
        }

        private void QuickAddCredit(String credit, float gap)
        {
            SpriteFont font = ((PoolGame)Game).fontDatabase.loadFont("Courier New");
            credits.Add(new Credit(credit, new Vector2(0, flCreditLastPos), font));
            flCreditLastPos += (gap + font.LineSpacing);
        }

        private void QuickAddCredit(String credit)
        {
            SpriteFont font = ((PoolGame)Game).fontDatabase.loadFont("Courier New");
            credits.Add(new Credit(credit, new Vector2(0, flCreditLastPos), font));
            flCreditLastPos += font.LineSpacing;
        }

        public override void OnOpen()
        {
            yPos = 0.0f;
            imageTimer = 4.0f;
            iListPosCur = 0;
            base.OnOpen();
        }

        public override void Update(GameTime gameTime)
        {
            if( iListPosCur > iPresidentsPos )
                yPos += ((float)(gameTime.ElapsedGameTime.TotalMilliseconds));
            else
                yPos += ((float)(gameTime.ElapsedGameTime.TotalMilliseconds/10));
            if (iListPosCur >= credits.Count)
            {
                if (imageTimer <= 0.0f)
                {
                    ((PoolGame)(Game)).menuManager.SwitchMenu(MenuManager.eMenuTypes.MENU_MAIN);
                    ((PoolGame)(Game)).eventManager.FireEvent(new PoolGame2.Managers.GameEvent((int)GameEventID.NIXON_SAY, "Thanks for playing\nOur Game"));
                }
                else
                {
                    imageTimer -= ((float)(gameTime.ElapsedGameTime.TotalMilliseconds) / 1000);
                }
            }
            else
            {
                if (yPos > credits[iListPosCur].pos.Y)
                    iListPosCur++;
            }

            base.Update(gameTime);
        }
 
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            
            PoolGame pGame = (PoolGame)Game;
            SpriteBatch spriteBatch = pGame.spriteBatch;
            Vector2 screenPosOffset = new Vector2(Game.GraphicsDevice.Viewport.Width/2, -yPos);
            Color color = Color.White;
            
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            for (int i = iListPosCur; i < credits.Count; i++)
            {
                if (credits[i].pos.Y > (yPos + Game.GraphicsDevice.Viewport.Height))
                    break;
                
                Vector2 strlen = credits[i].font.MeasureString(credits[i].name);
                Vector2 pos = credits[i].pos + screenPosOffset - new Vector2(strlen.X / 2, 0);
                spriteBatch.DrawString(credits[i].font, credits[i].name, pos, color);
                if( credits[i].image != null )
                {
                    pos.X += credits[i].font.Spacing * (credits[i].name.Length + 1);
                    spriteBatch.Draw(credits[i].image,
                        pos - new Vector2(0, credits[i].image.Height / 2),
                        color);
                }
            }

            if (imageTimer < 4.0f)
            {
                spriteBatch.Draw(finalImage,
                    new Vector2(Game.GraphicsDevice.Viewport.Width / 2 - (finalImage.Width / 2),
                        Game.GraphicsDevice.Viewport.Height / 2 - (finalImage.Height / 2)), color);

                SpriteFont font = ((PoolGame)Game).fontDatabase.loadFont("CourierNewSubTitle");
                String info = "2009 - CS7010 - IET Group Project - Team 3 - Pool Game";
                Vector2 strlen = font.MeasureString(info);
                spriteBatch.DrawString( font, info,
                    new Vector2(Game.GraphicsDevice.Viewport.Width / 2 - (strlen.X / 2),
                        Game.GraphicsDevice.Viewport.Height / 2 + (finalImage.Height / 2) +(font.LineSpacing * 2)), color);
            }
            spriteBatch.End();
        }
    }
}
